package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import bus.LoginBus;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
import javax.swing.JPasswordField;

public class JFrameLogin extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtEmail;
	private JPasswordField txtPassword;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrameLogin frame = new JFrameLogin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JFrameLogin() {
		setResizable(false);
		setTitle("Login");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 505, 326);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		txtEmail = new JTextField();
		txtEmail.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtEmail.setBounds(203, 96, 179, 30);
		contentPane.add(txtEmail);
		txtEmail.setColumns(10);

		JLabel lblUsername = new JLabel("Email address:");
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblUsername.setBounds(105, 105, 92, 14);
		contentPane.add(lblUsername);

		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblPassword.setBounds(105, 151, 92, 14);
		contentPane.add(lblPassword);

		JButton btnLogin = new JButton("Login");
		btnLogin.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnLogin.setBounds(301, 196, 81, 30);
		btnLogin.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

				if (checkDataInput()) {
					String email = txtEmail.getText();
					String pass = txtPassword.getText();

					String respone = LoginBus.login(email, pass);
					if (respone == null) {
						showMessage("Could not connect to database!");
					} else if (respone.contains("Student")) {
						JFrameStudentMainPage.ID = respone.substring(respone
								.lastIndexOf("@#$") + 3);
						JFrameStudentMainPage jFrameStudentMainPage = new JFrameStudentMainPage();
						jFrameStudentMainPage.setVisible(true);
						JFrameLogin.this.dispose();
					} else

					if (respone.contains("Teacher")) {
						JFrameTeacherMainPage.ID = respone.substring(respone
								.lastIndexOf("@#$") + 3);
						JFrameTeacherMainPage jFrameTeacherMainPage = new JFrameTeacherMainPage();
						jFrameTeacherMainPage.setVisible(true);
						JFrameLogin.this.dispose();

					} else if (respone.contains("Admin")) {
						JFrameAdminMainPage.ID = respone.substring(respone
								.lastIndexOf("@#$") + 3);
						JFrameAdminMainPage jFrameAdminMainPage = new JFrameAdminMainPage();
						jFrameAdminMainPage.setVisible(true);
						JFrameLogin.this.dispose();

					} else
						showMessage("Invalid email address or password!");

				}
			}
		});
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		contentPane.add(btnLogin);

		txtPassword = new JPasswordField();
		txtPassword.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtPassword.setBounds(203, 137, 179, 30);
		contentPane.add(txtPassword);
	}

	private void showMessage(String mess) {
		JOptionPane.showMessageDialog(this, mess);
	}

	private boolean checkDataInput() {
		if (txtEmail.getText().equals("")) {
			showMessage("Please enter email address!");
			return false;
		}
		if (txtPassword.getText().equals("")) {
			showMessage("Please enter email address!");
			return false;
		}
		return true;
	}
}
